package com.google.code.jtasks.client.ui.commands;

import com.google.code.jtasks.client.services.TaskService;
import com.google.code.jtasks.client.ui.Command;
import com.google.code.jtasks.client.ui.ScreenHelper;
import com.google.code.jtasks.client.ui.TaskOutputter;
import com.google.code.jtasks.common.data.Task;
import com.google.inject.Inject;

import java.util.List;

/**
 * Команда получения списка задач. Запрашивает сервис и печатает результат.
 */
public class ListTasksCommand implements Command {
    private final ScreenHelper helper;
    private final TaskOutputter taskOutputter;
    private final TaskService taskService;

    @Inject
    ListTasksCommand(ScreenHelper helper, TaskOutputter taskOutputter, TaskService taskService) {
        this.helper = helper;
        this.taskOutputter = taskOutputter;
        this.taskService = taskService;
    }

    @Override
    public String name() {
        return "List tasks";
    }

    @Override
    public String description() {
        return "Loads and shows all tasks on the server";
    }

    @Override
    public void execute() {
        List<Task> tasks = taskService.loadAllTasks();
        int y = 2;
        if (tasks.isEmpty()) {
            helper.drawString(5, y, "No tasks found");
            y += 2;

        } else {
            helper.drawString(5, y, "Found these tasks:");
            y += 2;
            y = taskOutputter.printTasks(y, tasks);

        }

        helper.waitForKey(y);
    }
}
